package com.F04.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.F04.actionForm.F0401ActionForm;
import com.F04.actionForm.F0403ActionForm;
import com.F04.dao.F0403Dao;
import com.action.BaseAction;
import com.config.Constants;
import com.exception.SelectException;
import com.util.CommonTools;
import com.util.SessionUtils;

public class F0403Action extends BaseAction {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		F0403ActionForm actionForm = (F0403ActionForm) form;
		F0403Dao dao = new F0403Dao();
		String operationType = actionForm.getOperationType();
		String language = SessionUtils.getLocale(request);
		actionForm.setLanguage(language);
		System.out.print("");
		try{
			
			/*--------------------------------------------主頁面操作begin--------------------------------------------------*/
			
//			初始化
			if(Constants.INIT.equals(operationType)){
				this.doINIT(actionForm);
				return mapping.findForward(Constants.List);
			}
			
//			检索
			if(Constants.QUERYLIST.equals(operationType)){
				List list = dao.select(actionForm);
				if(list!=null){
					actionForm.setList(list);
				}else{
					actionForm.setList(new ArrayList());
				}
				return mapping.findForward(Constants.List);
			}
			
//			新增初始化
			if(Constants.ADD.equals(operationType)){
				this.initActionForm(actionForm);
				return mapping.findForward(Constants.QUERYDETAIL);
			}
			
//			编辑初始化
			if(Constants.EDIT.equals(operationType)){
				actionForm.setFamilyCode(CommonTools.null2String(request.getParameter("code")));
				actionForm.setOldCode(actionForm.getFamilyCode());
				dao.selectByCode(actionForm);
				return mapping.findForward(Constants.QUERYDETAIL);
			}
			
//			删除
			if(Constants.DELETE.equals(operationType)){
				dao.delete(actionForm);
				return mapping.findForward("operateReturnList");
			}
			
			/*--------------------------------------------主頁面操作end------------------------------------------------------*/
			
			/*--------------------------------------------明细頁面操作begin--------------------------------------------------*/
			
//			新增保存
			if(Constants.SAVE_INSERT.equals(operationType)){
				dao.insert(actionForm);
				return mapping.findForward("operateReturnList");
			}
			
//			编辑保存
			if(Constants.SAVE_UPDATE.equals(operationType)){
				dao.update(actionForm);
				return mapping.findForward("operateReturnList");
			}
			
//			回显(新增和編輯保存后，返回主頁面)
			if("operateReturnList".equals(operationType)){
				this.initActionForm(actionForm);
				List list = dao.select(actionForm);
				if(list != null){
					actionForm.setList(list);
				}else{
					actionForm.setList(new ArrayList());
				}
				actionForm.setErrorFlag("0");
				return mapping.findForward(Constants.List);				
			}
			
//			明细页面返回主页面
			if("detailBack".equals(operationType)){
				actionForm.setErrorFlag("");
				this.initActionForm(actionForm);
				return mapping.findForward(Constants.List);
			}
			
			/*--------------------------------------------明细頁面操作end------------------------------------------------------*/
			
		}catch(Exception e){
			e.printStackTrace();
			dao.rollback();
			actionForm.setErrorFlag("3");
			return mapping.findForward(Constants.List);
		}finally{
			if(dao!=null){
				dao.disConnect();
			}
		}
		return mapping.findForward(Constants.List);
	}

	/**
	 * @初始�?
	 */
	public void doINIT(F0403ActionForm actionForm) throws SelectException {
		actionForm.setCurrentPage(1);
	}
	
	public void initActionForm(F0403ActionForm actionForm){
		actionForm.setFamilyCode("");
		actionForm.setFamilyCode1("");
		actionForm.setFamilyCode2("");
		actionForm.setFamilyName("");
		actionForm.setOldCode("");
	}
}
